package com.ssfeng.youxia.shiro;

import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.springframework.cache.Cache.ValueWrapper;

public class ShiroCache<K, V> implements Cache<K, V> {

	private org.springframework.cache.Cache cache;

	public ShiroCache(org.springframework.cache.Cache cache) {
		this.cache = cache;
	}

	@Override
	public void clear() throws CacheException {
		cache.clear();
	}

	@Override
	public V get(K key) throws CacheException {
		ValueWrapper valueWrapper = cache.get(key);
		if (valueWrapper == null) {
			return null;
		}
		return (V) valueWrapper.get();
	}

	@Override
	public Set<K> keys() {
		return null;
	}

	@Override
	public V put(K key, V value) throws CacheException {
		V previous = get(key);
		cache.put(key, value);
		return previous;
	}

	@Override
	public V remove(K key) throws CacheException {
		V previous = get(key);
		cache.evict(key);
		return previous;
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Collection<V> values() {
		return null;
	}

}
